package com.example.weatherforecast.citypagetool;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weatherforecast.MainActivity;
import com.example.weatherforecast.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author：coderChen
 * @Package：com.example.weatherforecast.tool
 * @Project：WeatherForecast
 * @Date：2023/12/31 21:16
 * @Filename：CityButtonAdapter
 * Explain:将存储的城市以按钮列表形式显示到页面
 */

public class ManageActivity_ButtonAdapter extends RecyclerView.Adapter<ManageActivity_ButtonAdapter.ViewHolder> {

    private List<String> cityList;
    private Context context;

    public ManageActivity_ButtonAdapter(List<String> cityList, Context context) {
        this.cityList = cityList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city_button, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String cityData = cityList.get(position);
        try {
            JSONObject cityObject = new JSONObject(cityData);
            String cityName = cityObject.getString("cityName");
            String cityCode = cityObject.getString("cityCode");
            holder.btnCity.setText(cityName);
            holder.btnCity.setOnClickListener(view -> {

                //发送城市编号
//                -----------------------------------------------
//                Intent intent = new Intent(context, MainActivity.class);
//                intent.putExtra("cityCode",cityCode);
//                context.startActivity(intent);
//                ------------------------------------------
                // 获取当前城市在列表中的索引
                int cityIndex = position;

                // 将所选城市的索引存储在 SharedPreferences 中
                SharedPreferences sharedPreferences = context.getSharedPreferences("CityIndex", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("selectedCityIndex", cityIndex);
                editor.apply();

                // 启动 MainActivity，并传递城市索引
                Intent intent = new Intent(context, MainActivity.class);
                intent.putExtra("cityIndex", cityIndex);
                context.startActivity(intent);
            });
            holder.btnCity.setOnLongClickListener(view -> {
                // 在这里处理长按事件，例如弹出删除确认对话框等
                return true;
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return cityList.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        Button btnCity;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            btnCity = itemView.findViewById(R.id.btnCity);
        }
    }
    public void removeCity(int position, SharedPreferences sharedPreferences) {
        cityList.remove(position);
        notifyItemRemoved(position);
        // 更新 SharedPreferences 中的城市数据
        Set<String> citySet = new HashSet<>(cityList);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putStringSet("cities", citySet);
        editor.apply();
    }
}
